thin.imports('/public/configure/conf.wx.js');
thin.imports('/public/plug/qrcodejs/qrcode.min.js');

thin.define(function(exports) {
	exports('submitScanCodePay', function() {
		var data_opt = {
			'project_id': $(this).attr('data-proid') || arguments[0],
			'pay_mark': $(this).attr('data-mark') || arguments[1]
		};
		var isAG = true;
		var money = 0,
			title = '',
			ele = '';
		if(data_opt.pay_mark == 'cp') {
			money = 398;
			title = '项目评估费';

			ele += '<p class="text-left text-underline position-fixed position-bottom m-b-1x">';
			ele += '<a href="javascript:;" id="btn-useCouponCode">使用优惠码</a>';
			ele += '</p>';

			$(document).off('click', '#btn-useCouponCode').on('click', '#btn-useCouponCode', function(e) {
				var e = e || window.event;
				e.stopPropagation();
				e.preventDefault();
				layer.prompt({
					title: '输入优惠码'
				}, function(value, index) {
					__getdata_order['code'] = value;
					layer.close(index);
				}, function() {
					delete __getdata_order['code'];
					layer.close(index);
				});
			});

		} else if(data_opt.pay_mark == 'cx') {
			money = 998;
			title = '缴纳诚信保证金';

			ele += '<div class="agree fs14r text-left text-underline position-fixed position-bottom m-b-1x">';
			ele += '<input type="checkbox" class="v-middle" id="weuiAgree" checked="checked">';
			ele += '<p style="display:inline-block;">';
			ele += '我已阅读并接受';
			ele += '<a href="javascript:showAGM();" class="c-3688E9">《诚信保证金协议》</a>';
			ele += '</p>';
			ele += '</div>';

			// 监听保证金协议
			$(document).off('click', '#weuiAgree').on('change', '#weuiAgree', function() {
				var ischecked = $(this).prop('checked');
				if(ischecked) {
					$('.layui-layer-btn0').removeClass('disabled');
					isAG = true;
				} else {
					$('.layui-layer-btn0').addClass('disabled');
					isAG = false;
				}
			});

		} else {
			return;
		}

		var str = '';
		str += '<p class="fs16r text-center m-t-6x c-888">' + title + '</p>';
		str += '<p class="fs16r text-center m-t-2x c-888">金额： <font class="c-f57c00">' + money + '</font> 元</p>';
		str += ele;

		layer.open({
			type: 0,
			title: false,
			content: str,
			area: ['480px', '240px'],
			btn: ['确定', '取消'],
			yes: function(index) {
				if(isAG) {
					layer.load(0, {
						shade: [0.08, '#000']
					});
					// 开始下单
					__getdata_order = tool.optionExtend(__getdata_order, data_opt);
					__getdata_order['title'] = title;
					__getdata_order['tradeType'] = 'NATIVE';
					factory.request('_wechat_order');
					layer.close(index);
				} else {
					return false;
				}
			},
			btn2: function(index) {
				layer.close(index);
			}
		});
	});
});

var __getdata_order = {},
	__getdata_notify = {},
	downNumber = 0;

// 下单回调
function __complete_order(res) {
	layer.closeAll('loading');
	if(res.status == 201 && res.data.code_url) {
		scanCodePay(res.data.code_url);
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 扫码支付
function scanCodePay(url) {
	var ele = '';
	ele += '<div class="position-relative">';
	ele += '<p class="fs18r c-888 text-center m-y-2x">请使用微信扫描二维码支付</p>';
	ele += '<div id="qrcode-pay" style="width:256px; overflow:hidden;" class="m-x-auto"></div>';
	ele += '<div id="pay-status" class="position-absolute position-bottom position-screen text-center" style="display: none; line-height: 256px; background-color: rgba(255, 255, 255, 0.9); color: green; font-size: 80px;"><i class="icon-ok"></i></div>';
	ele += '</div>';
	ele += '';

	layer.open({
		type: 0,
		title: false,
		content: ele,
		area: ['480px', '400px'],
		btn: false,
		end: function() {
			downNumber = 0;
		}
	});
	var qrcode = new QRCode(document.getElementById('qrcode-pay'), {
		text: url,
		width: 256,
		height: 256,
		colorDark: '#000000',
		colorLight: '#ffffff',
		correctLevel: QRCode.CorrectLevel.H
	});

	// 查询支付结果
	__getdata_notify['project_id'] = __getdata_order.project_id;
	__getdata_notify['pay_mark'] = __getdata_order.pay_mark;
	setTimeout(function() {
		downNumber = 1440;
		factory.request('_wechat_notify');
	}, 8000);
}

// 查询支付结果回调
function __complete_notify(res) {
	if(res.status == 200) {
		if(res.data == 1) {
			document.getElementById('pay-status').style.display = 'block';
			setTimeout(function() {
				// 扫码支付结果成功回调
				submitScanCodePayCallback && submitScanCodePayCallback();
			}, 1600);
			return;
		}
	}
	// 没有结果，继续查单
	--downNumber;
	if(downNumber > 0) {
		setTimeout(function() {
			factory.request('_wechat_notify');
		}, 5000);
	} else {
		layer.closeAll();
		layer.msg('支付失败', {
			time: 1600
		});
	}
}

// 显示诚信保证金协议
function showAGM() {

	var str = '';
	str += '<p style="text-align:center;line-height:40px; padding-top: 32px;">';
	str += '<strong><span style="font-size: 18px;font-family:宋体;color:#333333">《诚信保证金协议》</span></strong>';
	str += '</p>';
	str += '<p style="margin-bottom: 10px;line-height: 24px; padding: 24px;">';
	str += '如果客户没有对接资金成功，申请退款，经过我们审核后原路退回款项。如果客户成功融资，我方收取5%的中介费，所交保证金作为中介费之抵扣。如果客户不缴纳中介费，我方将该客户列入黑名单，并告知各投资机构，该客户的后续融资将很难进行。';
	str += '</p>';

	layer.open({
		type: 1,
		title: false,
		closeBtn: false,
		area: ['560px', '400px'],
		shade: 0.8,
		id: 'LAY_layuipro',
		resize: false,
		scrollbar: false,
		btn: ['关闭'],
		btnAlign: 'c',
		moveType: 1,
		content: str
	});
}